package com.chengqj.cglib.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
/**
 * @program: proxy
 * @description: cglib的代理类
 * @author: chengqj
 * @create: 2018-07-27 11:42
 **/
public class CglibProxy implements MethodInterceptor {
    public Object getInstance(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        //要把哪个设置为即将生成的新类父类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return  enhancer.create();
    }

    public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("前期物色");
        methodProxy.invokeSuper(object,args);
        System.out.println("后期跟踪");
        return null;
    }
}
